import { ref } from 'vue';
import { useLoginApi } from '@/api';
import { RKUtils } from '@/utils';

// 获取图形验证码

export const useImageCaptchaHook = () => {
	const captchaUuid = ref('');
	const captchaImageSrc = ref('');
	const { getCaptcha } = useLoginApi();
	/**
	 * 加载图片验证码
	 */
	const loadImageCaptcha = () => {
		captchaUuid.value = RKUtils.getGuid();
		getCaptcha(captchaUuid.value)
			.then((res) => {
				const { code, data, msg } = res;
				if (code === 200) {
					captchaImageSrc.value = 'data:image/png;base64,' + data;
				} else {
					console.error(msg);
				}
			})
			.catch((err) => {
				console.error(err);
			});
	};

	return {
		captchaUuid,
		captchaImageSrc,
		loadImageCaptcha,
	};
};
